package com.haiyi.software.app.basic;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class FragmentBasicSendRecords extends FragmentBasic {

	private final static String TAG = "FragmentBasicSendRecords";

	private final static String str_requestName = "sendRecords.php";

	private JsonObject json_return = null;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	protected List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

	protected Integer page = 1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void requestNetworkData() {

		String str_requestURL = str_server + str_requestName;

		AsyncHttpClient client = new AsyncHttpClient();

		RequestParams params = new RequestParams();

		params.put("args", getJsonArgs());

		client.post(str_requestURL, params, responseHandler);
	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			try {
				String str_Return = new String(responseBody, "UTF-8");

				JsonParser parser = new JsonParser();

				Log.i(TAG, str_Return);

				json_return = parser.parse(str_Return).getAsJsonObject();

				if (json_return.get("state").getAsString().equals("200")) {

					JsonArray jsonArray = json_return.get("body")
							.getAsJsonObject().get("giftList").getAsJsonArray();

					Type type = new TypeToken<Map<String, String>>() {
					}.getType();

					Gson gson = new Gson();

					list.clear();

					for (int i = 0; i < jsonArray.size(); i++) {
						JsonElement el = jsonArray.get(i);

						Map<String, Object> tmp = gson.fromJson(el, type);
						list.add(tmp);
					}

				}

				Log.i(TAG, list.toString());

				data.clear();

				data.addAll(list);

				requestFinish();

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
				requestError();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			requestError();
		}
	};

	private String getJsonArgs() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> argsBody = new HashMap<String, Object>();

			argsBody.put("page", page);

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", argsBody);

			Gson gson = new Gson();

			String s = gson.toJson(args);

			// Log.i(TAG, s);

			return s;
		} else {
			return null;
		}
	}
}
